<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午2:03
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\Redis;

class RoomHostMicro extends Base
{

    private $redis_room_hostmicro_key = "ROOM:HOSTMICRO:"; //ROOM:HOSTMICRO:{room_id}
    private $redis_room_hostmicro_user_key = "ROOM:HOSTMICROUSER:"; //ROOM:MICROUSER:{room_id}
    private $redis_room_hostmicro_into_begin_key = "ROOM:HOSTMICROINTOBEGIN:"; //ROOM:hostmicro_into_begin:{room_id}-{user_id}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function remove($room_id){
        $redis_key = $this->redis_room_hostmicro_user_key . $room_id;
        Redis::DEL($redis_key);
        $key = $this->redis_room_hostmicro_key . $room_id;
        Redis::DEL($key);
    }


    /**
     * 设置用户上麦状态
     * @param $room_id
     * @param $user_id
     * @param $status 0.上麦中、1.已经上麦
     * @return bool
     */
    public function set_user_status($room_id, $user_id, $status){
        $redis_key = $this->redis_room_hostmicro_user_key . $room_id;
        Redis::HSET($redis_key, $user_id, $status);
        return true;
    }

    /**
     * 获取用户上麦状态
     * @param $room_id
     * @param $user_id
     * @return int|null 0.上麦中、1.已经上麦
     */
    public function get_user_status($room_id, $user_id){
        $redis_key = $this->redis_room_hostmicro_user_key . $room_id;
        $ret = Redis::HGET($redis_key, $user_id);
        if ($ret === null){
            return null;
        }
        return intval($ret);
    }

    /**
     * 移除用户上麦状态
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function del_user_status($room_id, $user_id){
        $redis_key = $this->redis_room_hostmicro_user_key . $room_id;
        Redis::HDEL($redis_key, $user_id);
        return true;
    }


    /**
     * 获取主持麦用户ID
     * @param $room_id
     * @return int|null
     */
    public function get_user_id($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        $user_id = Redis::HGET($key, "user_id");
        if (empty($user_id)){
            return null;
        }
        return intval($user_id);
    }

    /**
     * 设置主持麦用户
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function set_user_id($room_id, $user_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        Redis::HSET($key, "user_id", $user_id);
        Redis::HSET($key, "into_time", time());
        $this->set_volume_open($room_id);
        $this->del_gift($room_id);

        return true;
    }

    /**
     * 删除麦
     * @param $room_id
     * @return bool
     */
    public function del_user_id($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        Redis::HDEL($key, "user_id");
        Redis::HDEL($key, "into_time");

        return true;
    }


    /*
     * 获取上麦时间
     */
    public function get_into_time($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        $into_time = Redis::HGET($key, "into_time");
        if (empty($into_time)){
            return null;
        }
        return intval($into_time);
    }


    /**
     * 获取麦状态
     * @param $room_id
     * @return bool
     */
    public function get_volume($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        $volume = Redis::HGET($key, "volume");
        if (empty($volume)){
            return true;
        }
        if (intval($volume) == 2){
            return false;
        }
        return true;
    }


    /**
     * 设置麦克风开
     * @param $room_id
     * @return bool
     */
    public function set_volume_open($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        Redis::HSET($key, "volume", 1);
        return true;
    }

    /**
     * 设置麦克风关
     * @param $room_id
     * @return bool
     */
    public function set_volume_close($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        Redis::HSET($key, "volume", 2);
        return true;
    }


    /**
     * 设置上麦前用户麦关系
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function set_into_begin($room_id, $user_id){
        $key = $this->redis_room_hostmicro_into_begin_key . $room_id . '-' . $user_id;
        Redis::SET($key, 0, 'EX', 10);
        return true;
    }

    /**
     * 删除上麦前用户麦关系
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function del_into_begin($room_id, $user_id){
        $key = $this->redis_room_hostmicro_into_begin_key . $room_id . '-' . $user_id;
        Redis::DEL($key);
        return true;
    }

    /**
     * 获取上麦前用户麦
     * @param $room_id
     * @param $user_id
     * @return int|null
     */
    public function get_into_begin($room_id, $user_id){
        $key = $this->redis_room_hostmicro_into_begin_key . $room_id . '-' . $user_id;
        $ret = Redis::GET($key);
        if ($ret === null){
            return null;
        }
        return intval($ret);
    }


    /**
     * 设置麦礼物
     * @param $room_id
     * @param $gift_id
     * @param $gift_name
     * @param $gift_img
     * @param $gift_num
     * @param $gift_price
     */
    public function set_gift($room_id, $user_id, $user_nickname, $user_image, $gift_id, $gift_name, $gift_img, $gift_num, $gift_price){
        $key = $this->redis_room_hostmicro_key . $room_id;
        $data = [
            'id' => $gift_id,
            'name' => $gift_name,
            'img' => $gift_img,
            'num' => $gift_num,
            'price' => $gift_price,
            'user_id' => $user_id,
            'user_nickname' => $user_nickname,
            'user_image' => $user_image
        ];
        Redis::HSET($key, "gift", json_encode($data));
    }

    /**
     * 删除礼物
     * @param $room_id
     * @return bool
     */
    public function del_gift($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        Redis::HDEL($key, "gift");
        return true;
    }

    /**
     * 获取麦礼物信息
     * @param $room_id
     * @return mixed|null
     */
    public function get_gift($room_id){
        $key = $this->redis_room_hostmicro_key . $room_id;
        $data = Redis::HGET($key, "gift");
        if (empty($data)){
            return null;
        }
        return json_decode($data, true);
    }

}